<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-12-06
 * Time: 16:13
 */

namespace app\admin\controller;


use app\model\AppComment;
use app\util\ReturnCode;
use think\exception\DbException;

class Comment extends Base
{

    public function getList()
    {
        $goodsId = $this->request->param('goods_id');
//        if (!isset($goodsId)) {
//            return $this->buildFailed(ReturnCode::INVALID, '无效的参数');
//        }
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $type = $this->request->get('type', '');
        $keywords = $this->request->get('keywords', '');
        $where = ['del' => 0];

        $obj = new AppComment();
        if ($type) {
            switch ($type) {
                case 1:
                    $obj = $obj->whereLike('goods_title', '%' . $keywords . '%');
                    break;
            }
        }
        if (isset($goodsId)) {
            $where['goods_id'] = $goodsId;
        }
        $where = $this->addTid($where);
        try {
            $listObj = $obj->where($where)->order('create_time ASC')->field('id,rate,imgs,content,create_time,uid,goods_title')->paginate($limit, false, ['page' => $start])->each(function ($value, $key) {
                $value->userInfo;
            })->toArray();
            return $this->buildSuccess([
                'list' => $listObj['data'],
                'count' => $listObj['total']
            ]);
        } catch (DbException $e) {
        }
        return $this->buildSuccess([
            'list' => null,
            'count' => 0
        ]);

    }
    public function getDetail(){
        $id = $this->request->param('id');
        if (!isset($id)) {
            return $this->buildFailed(ReturnCode::INVALID, '无效的参数');
        }
        $model = AppComment::get($id);
        if (!$model) {
            return $this->buildFailed(ReturnCode::INVALID, '数据不存在');
        }
        if ($model->del === 1) {
            return $this->buildFailed(ReturnCode::INVALID, '已经删除了');
        }
        $data = $model->getData();
        $data['userInfo']=$model->userInfo;
        return $this->buildSuccess($data);
    }

    public function del()
    {
        $id = $this->request->param('id');
        if (!isset($id)) {
            return $this->buildFailed(ReturnCode::INVALID, '无效的参数');
        }

        $model = AppComment::get($id);
        if (!$model) {
            return $this->buildFailed(ReturnCode::INVALID, '数据不存在');
        }
        if ($model->del === 1) {
            return $this->buildFailed(ReturnCode::INVALID, '已经删除了');
        }
        $model->setAttr('del', 1);
        $done = $model->allowField(true)->save();
        if ($done) {
            return $this->buildSuccess(null);
        }
        return $this->buildFailed(ReturnCode::FAILED, '操作失败');
    }
}